package com.sangu.dianxiu.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.utils.BaseDataManager;

import java.util.HashMap;

public class FeedbackActivity extends BaseActivity {

    private BaseDataManager baseDataManager=new BaseDataManager();
    @ViewInject(R.id.et_activity_feedback_content)  //使用XUtils注解，这样就不用findViewById了
    private EditText etContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ViewUtils.inject(this);
    }
    //使用XUtils的点击事件注解，这样就不用setOnClickListener了
    @OnClick({R.id.iv_activity_feedback_back,R.id.btn_activity_feedback_submit})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.iv_activity_feedback_back:
                finish();
                break;
            case R.id.btn_activity_feedback_submit:
                submit();
                break;
        }
    }

    private void submit() {
        if(TextUtils.isEmpty(etContent.getText().toString()))
        {
            Toast.makeText(this,"请输入反馈意见",Toast.LENGTH_SHORT).show();
        }else{
            HashMap<String,String> map=new HashMap<>();
            map.put("userId", BaseApp.user.getUserId()+"");
            map.put("feedback",etContent.getText().toString());
            baseDataManager.uploadUserFeedback(map, new ICallBack() {
                @Override
                public void onSuccess(String string) {
                    Toast.makeText(FeedbackActivity.this,"提交反馈成功",Toast.LENGTH_SHORT).show();           //提交反馈成功则提示用户
                }

                @Override
                public void onError(VolleyError error) {

                }
            });
        }
    }

    //在Activity退出的时候取消网络请求，以减少流量消耗，这也正是volley的优点之一
    @Override
    protected void onDestroy() {
        super.onDestroy();
        baseDataManager.requestQueue.cancelAll(BaseDataManager.class);
    }
}
